# This is the job submission script to resample the AWAP ASCII files of daily max and min temp
# To 250 m resolution for the WT plus Buffer regions

# Establish in directory

in.dir = '/home1/99/jc152199/MicroclimateStatisticalDownscale/AWAPASCII/RawAWAP/'
out.dir = '/home1/99/jc152199/MicroclimateStatisticalDownscale/250mASCII/'
setwd(in.dir)

# Load necessary libraries
necessary=c('SDMTools','sp','rgdal')
#check if library is installed
installed = necessary %in% installed.packages()
#if library is not installed, install it
if (length(necessary[!installed]) >=1) install.packages(necessary[!installed], dep = T)
#load the libraries
for (lib in necessary) library(lib,character.only=T)

##### NB: This only needs to be done once to create the .csv file of base.pos, below is the procedure for how it was created
##### 	  but leave it commented out when you run the batch script

	# Start by creating an object with row/col positions for the WT plus buffer regions
	# Then get XY's to obtain easting/northing
	# Then convert easting/northing to lat/long, storing in the same data frame
	# Then write out this file as a .csv to be imported for each job created by the loop at the bottom of this script

	# Read in base ASCII file that is datum WGS1984-UTM, 250 m res, and sized to WTplusBuffer
	base.asc = read.asc('/home1/99/jc152199/MicroclimateStatisticalDownscale/250mASCII/Analysis_Mask/Analysis_Mask_ASCII/AnalysisMask_WTplusbuffer_LatLong_WGS1984_250mres.asc')
	base.asc[,]=NA
	# Establish row/column positions for 250 m grid
	base.pos = as.data.frame(which(is.na(base.asc), arr.ind = T)) 

	# Add east and north to this data frame of row/column positions
	base.pos$lat = getXYcoords(base.asc)$x[base.pos$row]
	base.pos$long = getXYcoords(base.asc)$y[base.pos$col]

	# Write out a .csv file of row/col positions east/north and lat/long for WT plus buffer
	write.csv(base.pos,file=paste(in.dir,'WTplusBuffer.rowcol.latlong.csv',sep=''), row.names=F)


#### Make a list of all files in in.dir with pattern '.grid.gz', these are the files to resample
	
tfiles = list.files(in.dir,pattern='.grid.gz',recursive=TRUE,full.names=TRUE)


# Establish a temporary directory to write out the .sh files and .Rout files to
# This keeps your workspace more orderly and should the script fuck up all error logs will be stored in one location

tmp.pbs = '/home1/99/jc152199/tmp.pbs/'
dir.create(tmp.pbs)
setwd(tmp.pbs)

# Input location of the script to be run by this batch script

script.file = '/home1/99/jc152199/MicroclimateStatisticalDownscale/AWAPASCII/RawAWAP/ascii.res.5k.to.250m.2run.r'

# Begin a loop to create an .sh  file for each AWAP layer to be resample and then submit the jobs to the HPC

for(tfile in tfiles) 

	{
	
	tname = basename(tfile) #Get the just the name of the ASCII file from the file list
	
	tname = gsub('.grid.gz','.sh', tname) #Use tname to create the name for the .sh file
	
	arg.tfile = paste('tfile="',tfile,'" ',sep='') # Define the argument for tfile 
	
	zz = file(tname,'w') # This command creates a file with the name 'tname' The 'w' command opens the file and writes the following lines of text into it
		
		cat('##################################\n',file=zz) # First blank line of .sh file ### Note the use of '\n', any text following this will be on the next line
		cat('#!/bin/sh\n',file=zz) # First command line for HPC
		cat('cd $PBS_O_WORKDIR\n',file=zz) # Sets the directory to write out summary files at the above directory 'tmp.pbs'
		cat("R CMD BATCH --no-save --no-restore '--args ",arg.tfile,"' ",script.file,' ',gsub('.sh','',tname),'.Rout \n',sep='',file=zz) # This line tells R not to save to the .Rdata file and also not to restore the environment from the .Rdata file, very important when batching or you will get multiple fuckups, also defines the arguments, says where script to run is located, and defines the name for the R.out file
		cat('##################################\n',file=zz) # Last blank line of .sh file
	
	close(zz) # Closes the .sh file that you've just written
	
	# The follow line submits the job to the HPC, leave it commented out until you're ready to RUN all the jobs\
	# Always run the script first without removing the comment, this will still generate the .sh files for you to check before running
	# Once you're happy with that then submit a single .sh file and make sure it runs through to completion before batching out the rest with the following line
	system(paste('qsub -m n ',tname,sep=''))

}

# Done